package cn.uikit.demo.app;

import android.content.Context;
import android.icu.text.SimpleDateFormat;
import android.util.Log;



import cn.uikit.demo.utils.CrashLogUtil;
import cn.uikit.demo.utils.LogToFile;


public class CrashHandler implements Thread.UncaughtExceptionHandler {
    private static final String TAG = "CrashHandler";
    private static final CrashHandler sInstance = new CrashHandler();
    private Thread.UncaughtExceptionHandler mDefaultHandler;

    public static CrashHandler getInstance() {
        return sInstance;
    }

    public void init(Context context) {
        mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        Thread.setDefaultUncaughtExceptionHandler(this);
    }


    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        try {
            String log = CrashLogUtil.INSTANCE.getCrashLog(ex);
            LogToFile.INSTANCE.logError("Sivin", log);
        } catch (Exception e) {
            Log.e(TAG, "Error handling crash", e);
        } finally {
            if (mDefaultHandler != null) {
                mDefaultHandler.uncaughtException(thread, ex);
            } else {
                android.os.Process.killProcess(android.os.Process.myPid());
            }
        }
    }
}